package com.yunxin.core.infra.persistence.repository.channel;

import com.yunxin.core.domain.model.channel.ChannelPromotionApiCustomerConfig;
import com.yunxin.core.domain.model.channel.ChannelPromotionApiCustomerConfigRepository;
import com.yunxin.core.infra.persistence.converter.ChannelConvertor;
import com.yunxin.core.infra.persistence.entity.channel.ChannelPromotionApiCustomerConfigDO;
import com.yunxin.core.infra.persistence.mapper.channel.ChannelPromotionApiCustomerConfigExtendMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

/**
 * @author zero°
 * @since 2023-12-14
 */
@Repository
@RequiredArgsConstructor
public class ChannelPromotionApiCustomerConfigRepositoryImpl implements ChannelPromotionApiCustomerConfigRepository {

    private final ChannelConvertor channelConvertor;

    private final ChannelPromotionApiCustomerConfigExtendMapper channelPromotionApiCustomerConfigExtendMapper;
    @Override
    public boolean saveChannelPromotionApiCustomerConfig(ChannelPromotionApiCustomerConfig channelPromotionApiCustomerConfig) {
        ChannelPromotionApiCustomerConfigDO channelPromotionApiCustomerConfigDO = channelConvertor.formChannelPromotionApiCustomerConfig(channelPromotionApiCustomerConfig);
        channelPromotionApiCustomerConfigDO.setLimitTotal(channelPromotionApiCustomerConfig.getLimitTotal());
        int result = channelPromotionApiCustomerConfigExtendMapper.insertSelective(channelPromotionApiCustomerConfigDO);
        return result > 0;
    }

    @Override
    public boolean updateChannelPromotionApiCustomerConfig(ChannelPromotionApiCustomerConfig channelPromotionApiCustomerConfig) {
        ChannelPromotionApiCustomerConfigDO channelPromotionApiCustomerConfigDO = channelConvertor.formChannelPromotionApiCustomerConfig(channelPromotionApiCustomerConfig);
        channelPromotionApiCustomerConfigDO.setLimitTotal(channelPromotionApiCustomerConfig.getLimitTotal());
        int result = channelPromotionApiCustomerConfigExtendMapper.updateByPrimaryKeySelective(channelPromotionApiCustomerConfigDO);
        return result > 0;
    }

    @Override
    public ChannelPromotionApiCustomerConfig getChannelPromotionApiCustomerConfigByPromotionCode(String promotionCode) {
        ChannelPromotionApiCustomerConfigDO channelPromotionApiCustomerConfigDO = channelPromotionApiCustomerConfigExtendMapper.selectChannelPromotionApiCustomerConfigByPromotionCode(promotionCode);
        return channelConvertor.toChannelPromotionApiCustomerConfig(channelPromotionApiCustomerConfigDO);
    }
}
